package com.chujian.framework.idgen;

import cn.hutool.core.util.StrUtil;
import com.sankuai.inf.leaf.exception.InitException;
import com.sankuai.inf.leaf.service.SegmentService;
import com.sankuai.inf.leaf.service.SnowflakeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author zhaodong.xzd (github.com/yaccc)
 * @date 2019/10/09
 * @since support springboot starter with dubbo and etc rpc
 */
@Configuration
public class LeafSpringBootStarterAutoConfigure {

    private Logger logger = LoggerFactory.getLogger(LeafSpringBootStarterAutoConfigure.class);

    @Autowired
    private LeafSpringBootProperties properties;

    //// 从Spring配置中读取
    @Value("${spring.datasource.url}")
    private String dbUrl;
    @Value("${spring.datasource.username}")
    private String dbUser;
    @Value("${spring.datasource.password}")
    private String dbPassword;

    //// 系统本身的DataSource
    @Autowired(required = false)
    private DataSource dataSource;

    @Bean
    public SegmentService initLeafSegmentStarter() throws Exception {
        if (properties != null && properties.getSegment() != null && properties.getSegment().isEnable()) {
            SegmentService segmentService =
                    new SegmentService(
                            dataSource,
                            StrUtil.nullToDefault(properties.getSegment().getUrl(), dbUrl),
                            StrUtil.nullToDefault(properties.getSegment().getUsername(), dbUser),
                            StrUtil.nullToDefault(properties.getSegment().getPassword(), dbPassword)
                    );
            return segmentService;
        }
        logger.warn("init leaf segment ignore properties is {}", properties);
        return null;
    }

    @Bean
    public SnowflakeService initLeafSnowflakeStarter() throws InitException {
        if (properties != null && properties.getSnowflake() != null && properties.getSnowflake().isEnable()) {
            SnowflakeService snowflakeService = new SnowflakeService(properties.getSnowflake().getAddress(), properties.getSnowflake().getPort());
            return snowflakeService;
        }
        logger.warn("init leaf snowflake ignore properties is {}", properties);
        return null;
    }
}
